<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<input type="hidden" name="keyId" id="keyId" value="">
<div class="row" style="padding:2px">
	<div class="col-sm-12 col-md-4 col-lg-4">
			<h5><label style="width:100%"> <span class="label  label-success"> 提示：可以添加、删除、修改资源信息</span> </label>
			</h5><a id="ajax_permission_btn_add" class="btn btn-default " data-toggle="modal"><i class="fa fa-lg fa-plus"></i> 添加资源</a>
	</div>
</div>
<!-- rows -->
<!-- widget grid -->
<section id="widget-grid" class="">

	<!-- row -->
	<div class="row">

		<!-- NEW WIDGET START -->
		<article class="col-xs-12 col-sm-12 col-md-12 col-lg-12">

			<!-- Widget ID (each widget will need unique ID)-->
			<div class="jarviswidget" id="wid-id-0" data-widget-editbutton="false"
				data-widget-colorbutton="false" 
				data-widget-togglebutton="false" 
				data-widget-deletebutton="false" 
				data-widget-fullscreenbutton="true" 
				data-widget-custombutton="false" 
				data-widget-sortable="false">

				<header>
					<span class="widget-icon"> <i class="fa fa-table"></i> </span>
					<h2>个人权限 </h2>
					<div class="widget-toolbar">
                    </div>
				</header>

				<!-- widget div-->
				<div>
					<!-- widget edit box -->
					<div class="jarviswidget-editbox">
						<!-- This area used as dropdown edit box -->
					</div>
					<!-- end widget edit box -->

					<!-- widget content -->
					<div class="widget-body no-padding">
						<div id="ajax_permission_tree_div" class="col-xs-12 col-sm-4 col-md-4 col-lg-6" style="min-width:440px">
						</div>
                        <div id="ajax_permission_grid_div" class="col-xs-12 col-sm-8 col-md-8 col-lg-6" style="min-width:440px">
						</div>
						<div class="row">
						    <div class="col-sm-12 col-md-12 col-lg-5">
						        <div class="well well-light well-sm no-margin no-padding">
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <ul id="tree" class="ztree" style="max-height:500px;min-width:340px; overflow:auto;"></ul>
                                        </div>
                                    </div>
                                </div>
						    </div>
						    <div class="col-sm-12 col-md-12 col-lg-7">
                                <div class=" ">
                                    <div class="row" id="ajax_permission_list_row">
                                	    <table id="ajax_permission_list_table">
                                        </table>
                                		<div id="ajax_permission_list_page">
                                		</div>
                                	</div>
                                </div>
						    </div>
						</div>
					</div>
					<!-- end widget content -->

				</div>
				<!-- end widget div -->

			</div>
			<!-- end widget -->

		</article>
		<!-- WIDGET END -->

	</div>

	<!-- end row -->

	<!-- end row -->

</section>
<!-- end widget grid -->
<script type="text/javascript">
	// DO NOT REMOVE : GLOBAL FUNCTIONS!
	pageSetUp();
	function reload(){
		loadURL("ajax!permission.action",$('#content'));
	}
</script>
<script type="text/javascript">
    function loadCategoryTree(){
        var setting = {
            check: {
        	    enable: false
        	},
        	view: {
        	    dblClickExpand: false,
        		showLine: true,
        		selectedMulti: false
        	},
        	data: {
        	    simpleData: {
        		    enable:true,
        		    idKey: "id",
        		    pIdKey: "pId",
        		    rootPId: ""
        		}
        	},
        	callback: {
        	    onClick: onNodeClick1
        	}
        };
        $.ajax({
            type: "get",
        	url: 'ajax-permission!zTree.action',
        	success: function(data){
        	    if(data.result.errorCode == "1"){
        		    var t = $("#tree");
        			t = $.fn.zTree.init(t, setting, data.data.data);
        		}
        	}
        });
    }
    function onNodeClick1(event, treeId, treeNode){
        var permissionId = treeNode.id;
        jQuery("#ajax_permission_list_table").jqGrid("setCaption","资源列表 <i class='fa fa-arrow-circle-right'></i> "+treeNode.name);
      	jQuery("#ajax_permission_list_table").jqGrid('setGridParam',{postData:{cat:treeNode.intLevel , keyId : treeNode.id}});
      	jQuery("#ajax_permission_list_table").trigger("reloadGrid");
    }
    function fn_permission_edit(id){
        gDialog.fCreate({
            title:'更新资源信息',
        	url:"ajax-permission!input.action?keyId="+id,
        	width:500
        }).show();
    }

	//删除资源
	function fn_permission_delete(id){
		$.SmartMessageBox({
			title : "<i class='fa fa-minus-square txt-color-orangeDark'></i> <span class='txt-color-orangeDark'><strong>删除后该资源不可用</strong></span>",
			content : "确定删除该资源吗？",
			buttons : '[取消][确定]'
		}, function(ButtonPressed) {
			if (ButtonPressed === "确定") {
				var vActionUrl = "<%=path%>/com/ajax-permission!delete.action";
				data={keyId:id};
				ajax_action(vActionUrl,data,null,function(pdata){
					_show(pdata);
					jQuery("#ajax_permission_list_table").trigger("reloadGrid");
				});

			}
		});
	}
    function iconFormat(cellvalue, options, cell){

        return "<i class='fa fa-lg fa-fw "+cellvalue+"'></i>";
    }

    function run_jqgrid_function(){
        jQuery("#ajax_permission_list_table").jqGrid({
            url:'ajax-permission!jqGrid.action',
            datatype : "json",
            page : 1,
            colNames:['名称','父节点 ','层级','类别', '图标','关键字','编码', '路径','操作','Id'],
            colModel : [
                {name:'name',index:'name', width:50,sortable:false},
                {name:'pName',index:'pName', width:60,sortable:false},
                {name:'level',index:'level', width:40,sortable:false},
                {name:'type',index:'type', width:50,sortable:false,fixed:true},
                {name:'iconCls',index:'iconCls', width:50,sortable:false,fixed:true,formatter:iconFormat},
            	{name:'businessKey',index:'businessKey', width:80,search:false,fixed:true,sortable:false},
            	{name:'sortNumber',index:'sortNumber', width:40,search:false,sortable:false},
            	{name:'url',index:'url', width:100,search:false,fixed:true,sortable:false},
            	{name:'act',index:'act', width:100,sortable:false,fixed:true},
                {name:'id',index:'id',hidden:true,sortable:false,fixed:true},
            ],
            rowNum : 10,
            rowList:[10,20,30],
            pager : '#ajax_permission_list_page',
            sortname : 'id',
            sortorder : "asc",
            gridComplete:function(){
                $(".ui-jqgrid-bdiv").css("overflow-x","hidden");
                var ids=$("#ajax_permission_list_table").jqGrid('getDataIDs');
                for(var i=0;i<ids.length;i++){
                    var cl=ids[i];
                    se="<button class='btn btn-xs btn-default' data-original-title='编辑' onclick=\"fn_permission_edit('"+cl+"');\"><i class='fa fa-edit'></i></button>";
					se=se+"<button class='btn btn-xs btn-default' data-original-title='删除' onclick=\"fn_permission_delete('"+cl+"');\"><i class='fa fa-times'></i></button>";
                    jQuery("#ajax_permission_list_table").jqGrid('setRowData',ids[i],{act:se});
                }
                jqGridStyle();
            },
	        jsonReader: {
			    root: "dataRows",
			    page: "page",
			    total: "total",
			    records: "records",
			    repeatitems : false
			},
			caption : "详细信息 <i class='fa fa-arrow-circle-right'></i> ",
			multiselect : false,
            rownumbers:true,
            gridview:true,
            shrinkToFit:true,
            forceFit:true,
            viewrecords: true,
            height:"auto",
            autowidth: true,
            loadComplete: function() {
            }
        });
    }
    $(function(){
        loadCategoryTree();
        run_jqgrid_function();
    })
    $("a#ajax_permission_btn_add").unbind("click").bind("click",function(){
        gDialog.fCreate({
            title:'添加资源信息',
        	url:"ajax-permission!input.action?keyId=",
        	width:500
        }).show();
    })
    $(window).on('resize.jqGrid', function() {
        jQuery("#ajax_permission_list_table").jqGrid('setGridWidth', $("#ajax_permission_list_row").width());
    });

    jQuery("#ajax_permission_list_table").jqGrid('navGrid', "#ajax_permission_list_page", {
		edit : false,
		add : false,
		del : false,
		search:false
	});


</script>
